var app = getApp();
var memberUtil=require('../../utils/memberUtil');
Page({

  /**
   * 页面的初始数据
   */
  data: {
     pay:"0",
    submitFlagNum:0,
    chuMoney:0,
    showMoney:0,
      accountInfo:[]
  },
  //佣金提现
  bindPayTab: function (e) {
    var setPay = e.currentTarget.dataset.pay;
    this.setData({
      pay: setPay
    })
      this.findDrawcashAccount(setPay);
  },
  bindSave: function (e) {
    var that = this;
    var drawcashAmount = e.detail.value.drawcashAmount;
    if (drawcashAmount == "" || drawcashAmount * 1 < 100) {
      wx.showModal({
        title: '错误',
        content: '提现金额至少是100',
        showCancel: false
      })
      return false;
    }else if(drawcashAmount>that.data.monthcommissionAmount){
      wx.showModal({
        title: '错误',
        content: '提现金额不能大于你的账户余额',
        showCancel: false
      })
      return false;
    }
    var bankCardPersonName = e.detail.value.bankCardPersonName;
    if (bankCardPersonName == "" || bankCardPersonName==null) {
      wx.showModal({
        title: '错误',
        content: '姓名不能为空',
        showCancel: false
      })
      return false;
    }else if(!(/^[a-zA-Z\u4e00-\u9fa5]+$/.test(bankCardPersonName))){
      wx.showModal({
        title: '错误',
        content: '姓名只能输入汉字或字母',
        showCancel: false
      })
      return false;
    }
    var phoneNumber = e.detail.value.phoneNumber;
    if (phoneNumber == "" || phoneNumber == null) {
      wx.showModal({
        title: '错误',
        content: '手机号码不能为空',
        showCancel: false
      })
      return false;
    }else if(!(/^1[34578]\d{9}$/.test(phoneNumber))){
      wx.showModal({
        title: '错误',
        content: '请输入正确的手机号码',
        showCancel: false
      })
      return false;
    }

    var openingBank = e.detail.value.openingBank;
    var bankName = e.detail.value.bankName;
    var bankCardAccount = e.detail.value.bankCardAccount;
    var alipayAccount = e.detail.value.alipayAccount;
    if(that.data.pay==0){
      //银行名称
        if (bankName == "" || bankName==null){
            wx.showModal({
                title: '错误',
                content: '银行名称不能为空',
                showCancel: false
            })
            return false;
        }
      //银行开户行信息
      if (openingBank == "" || openingBank==null){
        wx.showModal({
          title: '错误',
          content: '银行开户行不能为空',
          showCancel: false
        })
        return false;
      }

      if (bankCardAccount == "" || bankCardAccount ==null) {
        wx.showModal({
          title: '错误',
          content: '银行卡号不能为空',
          showCancel: false
        })
        return false;
      }else if(!(/^\d{16,25}$/.test(bankCardAccount))){
        wx.showModal({
          title: '错误',
          content: '银行卡号只能为数字',
          showCancel: false
        })
        return false;
      }

    }else{
      //支付宝
      if (alipayAccount == "" || alipayAccount == null) {
        wx.showModal({
          title: '错误',
          content: '支付宝账号不能为空',
          showCancel: false
        })
        return false;
      }

    }


    var drawCashApplyParamJson={
      memberId: that.data.userInfo.memberId,
      drawcashType:that.data.pay,//0:银联，1：支付宝
      drawcashAmount: drawcashAmount,
      wechatAccount: "",
      alipayAccount: alipayAccount,
      bankCardAccount: bankCardAccount,
      bankCardPersonName: bankCardPersonName,
      openingBank: openingBank,
      phoneNumber: phoneNumber,
        bankName:bankName
    }
    if (that.data.submitFlagNum==0){
      wx.request({
        url: app.globalData.baseUrl + '/drawcash/apply',
        data: JSON.stringify(drawCashApplyParamJson),
        method: 'POST',
        success: function (res) {
          if (res.data.status == 200) {
          that.setData({
            submitFlagNum:1
          })
            wx.showModal({
              title: '成功',
              content: '您的提现申请已提交，等待财务打款',
              showCancel: false,
              success: function (res) {
                if (res.confirm) {
                  that.bindCancel();
                }
              }
            })
          } else {
            wx.showModal({
              title: '错误',
              content: res.data.msg,
              showCancel: false
            })
          }
        }
      })
    }else{
      wx.showModal({
        title: '提示',
        content: '您已经提交过申请了，请稍后再来吧',
        showCancel: false
      })
    }
  },
  bindCancel: function () {
    wx.navigateBack({})
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this;
    that.setData({
      userInfo:wx.getStorageSync("loginUserInfo").userInfo==undefined?"":wx.getStorageSync("loginUserInfo").userInfo,
    })
    if(that.data.userInfo.id==undefined) {
      memberUtil.getLogin(app,0,1,that,2);
    }

  },
  //全部提现
  allIn:function(){
    var that=this;
    var allMoney=Number(that.data.monthcommissionAmount);
    that.setData({
      showMoney:allMoney,
      chuMoney:Number(allMoney*0.02).toFixed(2)
    })
  },
  //change事件
  mycatch:function(e){
    var amonunt = Number(e.detail.value);
    this.setData({
      chuMoney:Number(amonunt*0.02).toFixed(2)
    })
  },
  //触发事件
  myfocus:function(){
    var that=this;
    this.setData({
      showMoney:""
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that=this;
    var memberIdJson = { memberId:that.data.userInfo.id}
    //调用我的佣金====每月收益
    wx.request({
      url: app.globalData.baseUrl + '/member/canDrawcashAmount',
      method: 'POST',
      data: JSON.stringify(memberIdJson),
      success: function (e) {
        if (e.statusCode == 200) {
          that.setData({
            monthcommissionAmount: e.data.data
          })
        }
      },
      fail: function () {
        wx.showToast({
          title: '系统开小差了请稍后再试！',
          icon: 'none',
          duration: 2000
        })
      }
    })
      that.findDrawcashAccount(0);
  },
    findDrawcashAccount:function(drawType){
    var that=this;
        var drawcashAccountJson={memberId:this.data.userInfo.id,drawcashType:drawType}
        wx.request({
            url: app.globalData.baseUrl + '/drawcash/findDrawcashAccount',
            method: 'POST',
            data: JSON.stringify(drawcashAccountJson),
            success: function (e) {
                if (e.statusCode == 200) {
                    that.setData({
                        accountInfo: e.data
                    })
                }
            },
            fail: function () {
                wx.showToast({
                    title: '系统开小差了请稍后再试！',
                    icon: 'none',
                    duration: 2000
                })
            }
        })
    },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var that=this;
    return {
      title: '我的二维码',
      path: "/pages/shop/shareSuccess?parentId=" + that.data.userInfo.id + "&enterType=0",
      imageUrl: app.globalData.loginUserInfo.erweimaUrl
    }
  }
})